package com.hxxy.wyp.petsLover.common.rest.base.backend;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hxxy.wyp.petsLover.RestBase;
import com.hxxy.wyp.petsLover.common.entities.Good;
import com.hxxy.wyp.petsLover.common.entities.User;
import com.hxxy.wyp.petsLover.common.entities.vo.req.GoodReq;
import com.hxxy.wyp.petsLover.common.entities.vo.req.HomeInfoReq;
import com.hxxy.wyp.petsLover.common.entities.vo.req.SearchObj;
import com.hxxy.wyp.petsLover.common.entities.vo.resp.GoodVo;
import com.hxxy.wyp.petsLover.common.services.base.backend.GoodService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName CommodityInfoRest
 * @Description TODO
 * @Author wyp
 * @Date 2023/1/17 22:48
 * @Version 1.0
 */
@RestController
@ResponseBody
@RequestMapping("/goodBack")
public class GoodRest extends RestBase {

    @Autowired
    private GoodService goodService;

    /**
     * 分页查询所有商品 带条件
     *
     * @param searchObj
     * @return
     */
    @RequestMapping("/all")
    @SaCheckLogin
    @SaCheckRole(value = {"commodity_admin", "super_admin"}, mode = SaMode.OR)
    @SaCheckPermission(value = {"all", "commodity_load"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    public Object pageAll(@RequestBody SearchObj searchObj) {

        Page page = goodService.pageSearch(searchObj);
        return ajaxSuccess(page, "200", "查询成功！");
    }

    /**
     * 修改商品上下架状态
     *
     * @param id
     * @return
     */
    @RequestMapping("/takeOffTheShelf")
    @SaCheckLogin
    @SaCheckRole(value = {"commodity_admin", "super_admin"}, mode = SaMode.OR)
    @SaCheckPermission(value = {"all", "commodity_update"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    public Object pageAll(@RequestParam("id") String id) {
        Good good = goodService.getById(Long.parseLong(id));
        if ("0".equals(good.getIsUpOrDown())) {
            good.setIsUpOrDown("1");
            goodService.updateById(good);
            return ajaxSuccess("200", "下架成功!");
        } else {
            good.setIsUpOrDown("0");
            goodService.updateById(good);
            return ajaxSuccess("200", "上架成功!");
        }
    }

    @RequestMapping("/deleteGood")
    @SaCheckLogin
    @SaCheckRole(value = {"commodity_admin", "super_admin"}, mode = SaMode.OR)
    @SaCheckPermission(value = {"all", "commodity_delete"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    public Object deleteGood(@RequestParam("id") String id) {
        boolean b = goodService.removeById(Long.parseLong(id));
        if (b) {
            return ajaxSuccess("200", "删除成功!");
        }
        return ajaxError("500", "删除失败!");
    }

    @RequestMapping("/addGood")
    @SaCheckLogin
    @SaCheckRole(value = {"commodity_admin", "super_admin"}, mode = SaMode.OR)
    @SaCheckPermission(value = {"all", "commodity_update"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    public Object addGood(@RequestBody GoodReq goodReq) {
        Good good = new Good();
        BeanUtils.copyProperties(goodReq, good);
        StringBuffer sb = new StringBuffer();
        for (HomeInfoReq homeInfoReq : goodReq.getFileList()) {
            sb.append(homeInfoReq.getUrl());
            sb.append(",");
        }
        good.setPic(sb.toString());
        boolean save = goodService.save(good);
        if (save) {
            return ajaxSuccess("200", "新增成功!");
        }
        return ajaxError("500", "新增失败!");
    }

    @RequestMapping("/updateGood")
    @SaCheckLogin
    @SaCheckRole(value = {"commodity_admin", "super_admin"}, mode = SaMode.OR)
    @SaCheckPermission(value = {"all", "commodity_update"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    public Object updateGood(@RequestBody GoodReq goodReq) {
        Good good = new Good();
        BeanUtils.copyProperties(goodReq, good);
        StringBuffer sb = new StringBuffer();
        for (HomeInfoReq homeInfoReq : goodReq.getFileList()) {
            sb.append(homeInfoReq.getUrl());
            sb.append(",");
        }
        good.setId(Long.parseLong(goodReq.getId()));
        if (goodReq.getFileList().size()<1){
            good.setPic(goodReq.getPic());
        }else {
            good.setPic(sb.toString());
        }
        boolean save = goodService.updateById(good);
        if (save) {
            return ajaxSuccess("200", "更新成功!");
        }
        return ajaxError("500", "更新失败!");
    }

    @RequestMapping("/findGoodById")
    @SaCheckLogin
    @SaCheckRole(value = {"commodity_admin", "super_admin"}, mode = SaMode.OR)
    @SaCheckPermission(value = {"commodity_load", "all"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    public Object findGoodById(@RequestParam("id") String id) {
        Good good = goodService.getById(id);
        GoodVo goodVo = new GoodVo();
        List<String> collect = Arrays.stream(good.getPic().split(",")).collect(Collectors.toList());
        BeanUtils.copyProperties(good,goodVo);
        goodVo.setId(id);
        goodVo.setPicList(collect);
        return ajaxSuccess(goodVo,"200");
    }
}
